package com.mxpio.erp.purchase.service.impl;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.purchase.entity.BuyRequest;
import com.mxpio.erp.common.purchase.entity.PurchaseOrder;
import com.mxpio.erp.common.purchase.entity.PurchaseOrderLine;
import com.mxpio.erp.common.purchase.service.BuyRequestService;
import com.mxpio.erp.common.purchase.service.PurchaseOrderLineService;
import com.mxpio.erp.common.purchase.service.PurchaseOrderService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.security.common.ContextUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class BuyRequestServiceImpl extends BaseServiceImpl<BuyRequest> implements BuyRequestService {

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	@Autowired
	private PurchaseOrderLineService purchaseOrderLineService;


	@Override
	@Transactional(readOnly = false)
	public String purc(String bizNos) {
		List<String> bizNoList = Arrays.asList(bizNos.split(","));
		List<BuyRequest> brList = JpaUtil.linq(BuyRequest.class).in("bizNo", bizNoList).list();
		if(CollectionUtils.isEmpty(brList)){
			return null;
		}

		long processingCount = brList.stream().filter(p->OrderStatusEnums.OrderStatus.FINISHED.getStatusCode().equals(p.getOrderStatus())).count();

		if(processingCount>0){
			return "请购单已执行";
		}

		long supplierCount = brList.stream().map(BuyRequest::getSupplierCode).distinct().count();
		if(supplierCount>1){
			return "供应商不唯一";
		}

		Date currentDate = new Date();

		PurchaseOrder purchaseOrder = new PurchaseOrder();
		purchaseOrder.setOrderDate(currentDate);
		purchaseOrder.setOrderType("01");
		purchaseOrder.setSupplierCode(brList.get(0).getSupplierCode());
		purchaseOrder.setSupplierName(brList.get(0).getSupplierName());
		purchaseOrder.setSalesman(ContextUtils.getLoginUsername());
		purchaseOrder.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
		purchaseOrder.setOrderStatus(OrderStatusEnums.OrderStatus.PLAN.getStatusCode());
		purchaseOrder.setBpmnStatus(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode());
		purchaseOrderService.save(purchaseOrder);

		String bizNo = purchaseOrder.getBizNo();
		for(int i=0;i<brList.size();i++){
			BuyRequest request = brList.get(i);
			PurchaseOrderLine line = new PurchaseOrderLine();
			line.setBizNo(bizNo);
			line.setLineNo(String.valueOf(i));
			line.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
			line.setSort(String.valueOf(i));
			line.setItemCode(request.getItemCode());
			line.setItemName(request.getItemName());
			line.setItemSpec(request.getItemSpec());
			line.setUnitCode(request.getUnitCode());
			line.setPrice(request.getPrice());
			line.setQuantity(request.getQuantity());
			line.setAcceptanceQuantity(BigDecimal.ZERO);
			line.setQualifiedQuantity(BigDecimal.ZERO);
			line.setUnqualifiedQuantity(BigDecimal.ZERO);
			line.setPlanedReceiving(BigDecimal.ZERO);
			line.setActualReceiving(BigDecimal.ZERO);
			line.setPlanedReturns(BigDecimal.ZERO);
			line.setSuggestOrderDate(request.getSuggestOrderDate());
			line.setSuggestArriveDate(request.getSuggestArriveDate());
			line.setReqBizNo(request.getBizNo());
			purchaseOrderLineService.save(line);
			request.setOrderStatus(OrderStatusEnums.OrderStatus.FINISHED.getStatusCode());
			this.update(request);
		}

		return null;
	}

}
